package com.ruoyi.system.facade;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BMedia;
import com.ruoyi.system.domain.BPostTags;
import com.ruoyi.system.domain.BPosts;
import com.ruoyi.system.service.IBCommentsService;
import com.ruoyi.system.service.IBMediaService;
import com.ruoyi.system.service.IBPostTagsService;
import com.ruoyi.system.service.IBPostsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BPostsFacade {
    @Autowired
    private IBPostsService postsService;
    @Autowired
    private IBPostTagsService tagsService;
    @Autowired
    private IBMediaService mediaService;
    @Autowired
    private IBCommentsService commentsService;

    /**
     * 添加文章
     * @param bPosts
     * @return
     */
    @Transactional
    public int addPosts(BPosts bPosts){
        if(StringUtils.isEmpty(bPosts.getCoverImg())){
            BMedia media = new BMedia();
            media.setRemark("defaultCoverImg");
            List<BMedia> bMedia = mediaService.selectBMediaList(media);
            if(!bMedia.isEmpty()){
                bPosts.setCoverImg(bMedia.get(0).getProxyPath());
            }
        }
        int count = postsService.insertBPosts(bPosts);
        List<String> tagsCodes = bPosts.getTagCodes();
        BPostTags tags = null;
        for (String tag : tagsCodes) {
            tags = new BPostTags();
            tags.setPostId(bPosts.getId());
            tags.setTag(tag);
            tagsService.insertBPostTags(tags);
        }
        return count;
    }

    /**
     * 修改文章
     * @param bPosts
     * @return
     */
    @Transactional
    public int updatePosts(BPosts bPosts){
        List<String> tagsCodes = bPosts.getTagCodes();
        BPostTags tags = null;
        tagsService.deleteBPostTagsByPostId(bPosts.getId());
        if(tagsCodes!=null) {
            for (String tag : tagsCodes) {
                tags = new BPostTags();
                tags.setPostId(bPosts.getId());
                tags.setTag(tag);
                tagsService.insertBPostTags(tags);
            }
        }
        if(StringUtils.isEmpty(bPosts.getCoverImg())){
            BMedia media = new BMedia();
            media.setRemark("defaultCoverImg");
            List<BMedia> bMedia = mediaService.selectBMediaList(media);
            if(!bMedia.isEmpty()){
                bPosts.setCoverImg(bMedia.get(0).getProxyPath());
            }
        }
        bPosts.setUpdateTime(new Date());
        return postsService.updateBPosts(bPosts);
    }

    /**
     * 查询文章详情
     * @param id
     * @return
     */
    public BPosts selectBPostsById(Long id){
        BPostTags tag = new BPostTags();
        tag.setPostId(id);
        List<BPostTags> tags = tagsService.selectBPostTagsList(tag);
        List<String> tagList = tags.stream().map(BPostTags::getTag).collect(Collectors.toList());
        BPosts bPosts = postsService.selectBPostsById(id);
        bPosts.setViewCount(bPosts.getViewCount()+1);
        postsService.updateBPosts(bPosts);
        bPosts.setTagCodes(tagList);
        return bPosts;
    }

    public List<BPosts> selectBPostsNews(){
        List<BPosts> bPosts = postsService.selectBPostsNews();
        BPostTags tag = null;
        for (BPosts p : bPosts) {
            tag = new BPostTags();
            tag.setPostId(p.getId());
            List<BPostTags> tags = tagsService.selectBPostTagsList(tag);
            List<String> tagList = tags.stream().map(BPostTags::getTag).collect(Collectors.toList());
            p.setTagCodes(tagList);
        }
        return bPosts;
    }

    public void selectListByTag(TableDataInfo data){
        List<BPosts> list = (List<BPosts>)data.getRows();
        BPostTags tag = null;
        for (BPosts p : list) {
            tag = new BPostTags();
            tag.setPostId(p.getId());
            List<BPostTags> tags = tagsService.selectBPostTagsList(tag);
            List<String> tagList = tags.stream().map(BPostTags::getTag).collect(Collectors.toList());
            p.setTagCodes(tagList);
        }
    }

    /**
     * 删除文章
     * @param ids
     */
    @Transactional
    public int deletePostsByIds(Long[] ids){
        tagsService.deleteBPostTagsByPostIds(ids);
        commentsService.deleteBCommentsByPostsIds(ids);
        return postsService.deleteBPostsByIds(ids);
    }
}
